package tt.dz.order.vo;

import org.apache.shiro.util.CollectionUtils;
import tt.dz.order.exception.BusinessException;
import tt.dz.order.pojo.OrderAlreadyPaid;
import tt.dz.order.pojo.OrderPayInfo;
import tt.dz.order.util.OrderConstant;
import tt.dz.order.util.page.DefaultPaginationBuilder;
import tt.dz.order.util.page.DefaultPaginationDirector;
import tt.dz.order.util.page.Pagination;
import tt.dz.order.util.page.PaginationDirector;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderPayLogVo {
    private static final String select_all_sql = "select a.*,b.name as  merchantName, oper.name as userName  from order_pay_info a left join ms_merchant b on a.merchantcode =b.merchant_code LEFT JOIN  pub_oper_list oper on oper.manager_id=a.userId where 1=1 ";
    // 运营商号
    private String merchantCode;
    private Date startDate;
    private Date endDate;


    /**
     * 订单修改日志查询
     *
     * @param queryVO
     * @param curPageNo
     * @param prePageNum
     * @returnqueryOrderPayLog
     * @throws BusinessException
     */
    public static Pagination<OrderPayInfo> queryPagition(OrderPayLogVo queryVO, int curPageNo,
                                                         int prePageNum) throws BusinessException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        StringBuilder controlBuilder = new StringBuilder();

        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (queryVO.getStartDate() != null) {
            controlBuilder.append("		AND  to_char(a.createTime,'yyyy-MM-dd') >=:startDate");
            paramMap.put("startDate", dateFormat.format(queryVO.getStartDate()));
        }
        if (queryVO.getEndDate() != null) {
            controlBuilder.append("		AND to_char(a.createTime,'yyyy-MM-dd') <=:endDate");
            paramMap.put("endDate", dateFormat.format(queryVO.getEndDate()));
        }
        if (queryVO.getMerchantCode() != null) {
            controlBuilder.append("		AND a.merchantcode =:merchantCode");
            paramMap.put("merchantCode", queryVO.getMerchantCode());
        }
        String countSql = " select count(1) from order_pay_info a where 1=1" + controlBuilder.toString();
        PaginationDirector director = new DefaultPaginationDirector(new DefaultPaginationBuilder(), countSql,
                select_all_sql + controlBuilder.append(" ORDER BY createTime DESC ").toString(), paramMap, curPageNo, prePageNum);
        Pagination<OrderPayInfo> pagination = director.construct(OrderPayInfo.class);
        List<OrderPayInfo> list = pagination.getList();
        // 转换其中的基础代码转换为名称
        if (!CollectionUtils.isEmpty(list)) {
            for (OrderPayInfo orderPayInfo : list) {

                if (orderPayInfo.getStatus() != null) {
                    orderPayInfo.setStatusName(OrderConstant.OrderStatus.getDesc(orderPayInfo.getStatus()));
                }
            }
        }
        return pagination;
    }

    public String getMerchantCode() {
        return merchantCode;
    }

    public void setMerchantCode(String merchantCode) {
        this.merchantCode = merchantCode;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
}
